<template>
    <div>
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span>攻略新增</span>
            </div>
            <el-form :model="form" :rules="rules" ref="ruleForm">
                <el-form-item label="标题" :label-width="formLabelWidth" prop="guide_name">
                    <el-input v-model="form.guide_name" autocomplete="off" placeholder="标题"></el-input>
                </el-form-item>
                <el-form-item label="景点" :label-width="formLabelWidth" prop="guide_place">
                    <el-input v-model="form.guide_place" autocomplete="off" placeholder="景点"></el-input>
                </el-form-item>
                <el-form-item label="攻略信息" :label-width="formLabelWidth" prop="guide_information">
                    <el-input type="textarea" v-model="form.guide_information" autocomplete="off" placeholder="攻略信息"
                        maxlength="1000" show-word-limit></el-input>
                </el-form-item>

                <el-form-item label="图片信息" :label-width="formLabelWidth">
                    <el-upload style="margin:10px 0px!important;" :action="uploadUrl" list-type="picture-card"
                        :on-preview="handlePictureCardPreview" :on-remove="handleRemove" :headers="headerObj"
                        :on-success="handleSuccess" :file-list="fileList">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible">
                        <img width="100%" :src="dialogImageUrl" alt="">
                    </el-dialog>
                </el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import api from '@/api'
export default {
    data() {
        //  正则表达式
        return {
            form: {
                guide_name: "",
                guide_information: "",
                guide_place: "",
                guide_createname: "",
                guide_createtime: "",
                guide_createid: "",
                guide_img: []
            },
            headerObj: {
                file: '',
            },
            dialogImageUrl: '',
            dialogVisible: false,
            disabled: false,
            //1.配置要上传文件的路由
            uploadUrl: "http://localhost:4000/file/uploadImg",
            fileList: [],
            formLabelWidth: '80px',
            rules: {
                guide_name: [
                    { required: true, message: '请输入种类', trigger: 'blur' },
                ],
                guide_information: [
                    { required: true, message: '请输入起点', trigger: 'blur' },
                ],
                guide_place: [
                    { required: true, message: '请输入终点', trigger: 'blur' },
                ],
            },
        }
    },
    methods: {
        handleSuccess(response, file, fileList) {
            this.form.guide_img.push(response.url)
        },
        handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url;
            // console.log(file, fileList, dialogImageUrl);
            this.dialogVisible = true;
        },
        handleRemove(file) {
            let index = this.form.guide_img.indexOf(file.response.url);
            if (index !== -1) {
                this.form.guide_img.splice(index, 1);
            }
            console.log(this.form);
        },
        async gettime() {
            let now = new Date();
            let year = now.getFullYear();
            let month = now.getMonth() + 1;
            month = month < 10 ? '0' + month : month;
            let date = now.getDate();
            date = date < 10 ? '0' + date : date;
            let hours = now.getHours();
            hours = hours < 10 ? '0' + hours : hours;
            let minutes = now.getMinutes();
            minutes = minutes < 10 ? '0' + minutes : minutes;
            let seconds = now.getSeconds();
            seconds = seconds < 10 ? '0' + seconds : seconds;
            let formattedDate = `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;
            this.form.guide_createtime = await formattedDate;
            this.form.guide_createid = '65ebdc472a75000065002c13';
            this.form.guide_createname = '官方攻略站'
        },
        async handleOk() {
            if (this.form.guide_img.length == 0) {
                this.$message.error('请上传图片');
            } else {
                this.gettime();
                console.log(this.form);
                setTimeout(async () => {
                    let res = await api.addGuide({ ...this.form })
                    if (res?.code == 200) {
                        this.$message({
                            message: res.message,
                            type: 'success'
                        });
                        this.$router.push('/guide/list')
                    } else {
                        this.$message.error(res.message);
                    }
                }, 0);
            }
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.handleOk();
                } else {
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style lang="scss" scoped>
.el-input,
.el-select {
    width: 340px;
}
</style>